home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / human interface toolbox / progressbars / menus.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  5.3 KB  |  279 lines

  1. /*
  2.     File:        Menus.c
  3.  
  4.     Contains:    Handles the application's menus
  5.  
  6.     Written by: Chris White    
  7.  
  8.     Copyright:    Copyright © 1996-1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 8/10/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  20.                 
  21.  
  22. */
  23.  
  24.  
  25. #pragma segment Core
  26.  
  27.  
  28. #ifndef __MENUS__
  29.     #include <Menus.h>
  30. #endif
  31.  
  32. #ifndef __WINDOWS__
  33.     #include <Windows.h>
  34. #endif
  35.  
  36. #ifndef __DIALOGS__
  37.     #include <Dialogs.h>
  38. #endif
  39.  
  40. #ifndef __TEXTUTILS__
  41.     #include <TextUtils.h>
  42. #endif
  43.  
  44. #ifndef __DESK__
  45.     #include <Desk.h>
  46. #endif
  47.  
  48.  
  49.  
  50.  
  51.  
  52. // Application includes
  53.  
  54. #ifndef __BAREBONES__
  55.     #include "BareBones.h"
  56. #endif
  57.  
  58. #ifndef __PROTOTYPES__
  59.     #include "Prototypes.h"
  60. #endif
  61.  
  62.  
  63.  
  64.  
  65.  
  66. // static includes
  67.  
  68. static void        DoAppleCmds ( SInt16 theMenu, SInt16 theItem );
  69. static void        DoFileCmds ( SInt16 theMenu, SInt16 theItem );
  70. static void        DoProgressBarsCmds ( SInt16 theMenu, SInt16 theItem );
  71. static Boolean    EnableFileCmds ( WindowRef frontWindow );
  72. static Boolean    EnableProgressBarsCmds ( WindowRef frontWindow );
  73.  
  74. static void        SetItemEnable ( MenuHandle theMenu, SInt16 theItem, Boolean bEnabled );
  75.  
  76.  
  77.  
  78.  
  79.  
  80. void MenuDispatch ( SInt32 menuResult )
  81. {
  82.     SInt16     theMenu = (menuResult >> 16);                    // menu selected
  83.     SInt16     theItem = (menuResult & 0x0000FFFF);               // item selected
  84.     
  85.     switch (theMenu)
  86.     {
  87.         case kAppleMenu: 
  88.             DoAppleCmds ( theMenu, theItem );
  89.         break;
  90.         
  91.         case kFileMenu: 
  92.             DoFileCmds ( theMenu, theItem );
  93.         break;
  94.         
  95.         case kProgressBarsMenu:
  96.             DoProgressBarsCmds ( theMenu, theItem );
  97.         break;
  98.         
  99.     }
  100.     
  101.     HiliteMenu ( 0 );               // un-hilite selected menu
  102.     
  103.     return;
  104.     
  105. } // MenuDispatch
  106.  
  107.  
  108.  
  109. void AdjustMenus ( void )
  110. {
  111.     Boolean        bRedraw = false;
  112.     WindowRef    frontWindow;
  113.     
  114.     frontWindow = FrontWindow ( );
  115.     if ( EnableFileCmds ( frontWindow ) )
  116.         bRedraw = true;
  117.     if ( EnableProgressBarsCmds ( frontWindow ) )
  118.         bRedraw = true;
  119.     
  120.     if ( bRedraw )
  121.         DrawMenuBar ( );
  122.     
  123.     return;
  124.     
  125. } // AdjustMenus
  126.  
  127.  
  128.  
  129. static void DoAppleCmds ( SInt16 theMenu, SInt16 theItem )
  130. {
  131.     #pragma unused(theMenu)
  132.     Str255    name;            // string for DA name
  133.     
  134.     
  135.     switch ( theItem )
  136.     {
  137.         case cAbout:
  138.             Alert ( kAboutDialog, nil );
  139.         break;
  140.         
  141.         default:
  142.             GetMenuItemText ( GetMenuHandle ( kAppleMenu ), theItem, (StringPtr) &name );
  143.             OpenDeskAcc ( (StringPtr) &name );
  144.         break;
  145.     }
  146.     
  147.     return;
  148.     
  149. } // DoAppleCmds
  150.  
  151.  
  152.  
  153. static void DoFileCmds ( SInt16 theMenu, SInt16 theItem )
  154. {    
  155.     #pragma unused(theMenu)
  156.     switch ( theItem )
  157.     {
  158.         case cQuit:
  159.             gQuit = true;
  160.         break;
  161.     }
  162.     
  163.     return;
  164.     
  165. } // DoFileCmds
  166.  
  167.  
  168.  
  169. static void DoProgressBarsCmds ( SInt16 theMenu, SInt16 theItem )
  170. {    
  171.     OSErr            theErr = noErr;
  172.     Str255            theText;
  173.     static Boolean    bUseThreads = false;
  174.     
  175.     
  176.     switch ( theItem )
  177.     {
  178.         case cToggleThreads:
  179.             bUseThreads = !bUseThreads;
  180.             CheckItem ( GetMenuHandle ( theMenu ), theItem, bUseThreads );
  181.         break;
  182.         
  183.         case cStandard:
  184.             GetIndString ( theText, kMiscStrings, kProgressStr );
  185.             if ( bUseThreads )
  186.                 theErr = ThreadedProgressOperation ( ThreadedStandardDemoOperation, nil,
  187.                                                                     theText, nil, false );
  188.             else
  189.                 theErr = ProgressOperation ( StandardDemoOperation, nil, theText );
  190.         break;
  191.         
  192.         case cBarberPole:
  193.             GetIndString ( theText, kMiscStrings, kProgressStr );
  194.             if ( bUseThreads )
  195.                 theErr = ThreadedProgressOperation ( ThreadedBarberPoleDemoOperation, nil,
  196.                                                                     theText, nil, true );
  197.             else
  198.                 theErr = ProgressOperation ( BarberPoleDemoOperation, nil, theText );
  199.         break;
  200.     }
  201.     
  202.     if ( theErr )
  203.         AlertUser ( kGenericErrorStr, theErr, "\p" );
  204.     
  205.     return;
  206.     
  207. } // DoProgressBarsCmds
  208.  
  209.  
  210.  
  211. static Boolean EnableFileCmds ( WindowRef frontWindow )
  212. {
  213.     static Boolean    bIsEnabled = true;
  214.     Boolean            bHasDialog = false;
  215.     Boolean            bUpdate = false;
  216.     MenuHandle        theMenu = GetMenuHandle ( kFileMenu );
  217.     
  218.     
  219.     bHasDialog = frontWindow && GetWindowKind ( frontWindow ) == dialogKind;
  220.     
  221.     if ( bIsEnabled == bHasDialog )        // Or bIsEnabled != !bHasDialog
  222.     {
  223.         SetItemEnable ( theMenu, 0, !bHasDialog );
  224.         bIsEnabled = !bHasDialog;
  225.         bUpdate = true;
  226.     }
  227.     
  228.     SetItemEnable ( theMenu, cQuit, !bHasDialog );
  229.     
  230.     return bUpdate;
  231.     
  232. } // EnableFileCmds
  233.  
  234.  
  235.  
  236. static Boolean EnableProgressBarsCmds ( WindowRef frontWindow )
  237. {
  238.     static Boolean    bIsEnabled = true;
  239.     Boolean            bHasDialog = false;
  240.     Boolean            bUpdate = false;
  241.     MenuHandle        theMenu = GetMenuHandle ( kProgressBarsMenu );
  242.     
  243.     
  244.     bHasDialog = frontWindow && GetWindowKind ( frontWindow ) == dialogKind;
  245.     
  246.     if ( bIsEnabled == bHasDialog )        // Or bIsEnabled != !bHasDialog
  247.     {
  248.         SetItemEnable ( theMenu, 0, !bHasDialog );
  249.         bIsEnabled = !bHasDialog;
  250.         bUpdate = true;
  251.     }
  252.     
  253.     SetItemEnable ( theMenu, cToggleThreads, gHasThreadManager );
  254.     
  255.     return bUpdate;
  256.     
  257. } // EnableProgressBarsCmds
  258.  
  259.  
  260.  
  261. static void SetItemEnable ( MenuHandle theMenu, SInt16 theItem, Boolean bEnabled )
  262. {
  263.     if ( theMenu )
  264.     {
  265.         if ( bEnabled )
  266.             EnableItem ( theMenu, theItem );
  267.         else
  268.             DisableItem ( theMenu, theItem );
  269.     }
  270.         
  271.     return;
  272.     
  273. } // SetItemEnable
  274.  
  275.  
  276.  
  277.  
  278.  
  279.